package com.hp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.entity.Admin;
import com.hp.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Author: LiuQiang
 * @Date: 2021/10/22 23:03
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @RequestMapping("/toAdminLogin")
    public String homePage(){
        return "admins/login";
    }

    /**
     * 访问请求，跳转页面
     * redirect:/login.jsp
     * @param session
     * @return
     */
    @GetMapping("/login")
    public String login(HttpSession session){
        session.removeAttribute("msg");
        return "admins/login";
    }

    /**
     * 验证登录
     * @param userName
     * @param passWord
     * @param session
     * @return
     */
    @PostMapping("/toLogin")
    public String adminLogin(@RequestParam("userName") String userName,
                             @RequestParam("passWord") String passWord,
                             HttpSession session,Model model) {
        int nameLength = userName.trim().length();
        int wordLength = passWord.trim().length();
        if (nameLength >= 2 && nameLength <= 20 & wordLength >= 2 && wordLength <= 20) {
            String reg = "^[A-Za-z0-9]+$";
            if (Pattern.matches(reg, userName) && Pattern.matches(reg, passWord)) {
                Admin admin = adminService.adminLogin(userName, passWord);
                if (null == admin) {
                    session.setAttribute("msg", "账号或密码错误");
                    return "admins/login";
                } else {
                    session.setAttribute("admin", admin);
                    model.addAttribute("msg","登录成功!");
                    return "admins/index";
                }
            } else {
                session.setAttribute("msg", "账号或密码格式错误");
                return "admins/login";
            }
        } else {
            session.setAttribute("msg", "账号或密码长度错误");
            return "admins/login";
        }
    }

    /**
     * 清除session，退出登录
     * @param session
     * @return
     */
    @RequestMapping("/exit")
    public String exit(HttpSession session){
        //清空session中的登录信息
        session.invalidate();//清空session
        //重新访问登录页面(转发，重定向)
        return "admins/login";
    }

    /**
     * 跳转index页面
     * @param session
     * @return
     */
    @GetMapping("/index")
    public String index(HttpSession session){
        session.removeAttribute("msg");
        return "admins/index";
    }

    /**
     * 查询全部信息
     * @param model
     * @return
     */
    @GetMapping("/adminList")
    public String adminList(int page,Model model){
        //查询全部信息
        PageHelper.startPage(page,5);
        List<Admin> adminList = adminService.findAllAdmins();
        PageInfo pageInfo = new PageInfo(adminList);
        model.addAttribute("adminList",adminList);
        model.addAttribute("pageInfo",pageInfo);
        return "admins/admin_list";
    }

    /**
     * 跳转新增页面
     * @return
     */
    @GetMapping("/toAdmin_add")
    public String toAdmin_add(){
        return "admins/admin_add";
    }

    /**
     * 新增信息
     * @param admin
     * @return
     */
    @PostMapping("/adminSave")
    public String adminSave(Admin admin){
        //新增信息
        adminService.adminSave(admin);
        //可在service层判断名称是否重复
        return "redirect:/admin/adminList?page=1";
    }

    /**
     * 根据id删除信息
     * @param id
     * @return
     */
    @GetMapping("/adminDelete")
    public String adminDelete(@RequestParam("id") Integer id){
        //根据id删除信息
        adminService.findAdminsByIdDelete(id);
        return "redirect:/admin/adminList?page=1";
    }

    /**
     * 根据id重置密码
     * @param id
     * @return
     */
    @GetMapping("/resetPassWord")
    public String resetPassWord(@RequestParam("id") Integer id){
        //根据id重置密码
        adminService.resetPassWord(id);
        return "redirect:/admin/adminList?page=1";
    }

}
